package com.dongdongshop.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.data.PageInfo;
import com.dongdongshop.pojo.comment.Comment;
import com.dongdongshop.service.comment.CommentServiceI;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Component;
import org.springframework.data.mongodb.core.query.Query;
import javax.annotation.Resource;
import java.util.List;

@Component
@Service
public class CommentServiceImpl implements CommentServiceI {

    @Resource
    private MongoTemplate mongoTemplate;

    /**
     * 根据商品id查询评论
     * @param goodsId
     */
    @Override
    public PageInfo<Comment> getCommentByGoodsId(Long goodsId, Integer pageNumber, Integer pageSize) {
        PageInfo<Comment> pageInfo = new PageInfo<>();
        pageInfo.setPageNumber(pageNumber);
        pageInfo.setPageSize(pageSize);
        Query query = new Query(Criteria.where("goodId").is(goodsId));
        Long count = mongoTemplate.count(query,Comment.class);
        pageInfo.setTotal(count);
        query.skip(pageInfo.getOffset()).limit(pageInfo.getPageSize());
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        //System.out.println("size"+commentList.size());
        pageInfo.setList(commentList);
        return pageInfo;
    }

    //添加评论
    @Override
    public void addComment(Comment comment) {
        mongoTemplate.insert(comment);
    }
}
